namespace CIS
{
    using CISLib;
    using System;
    using System.Drawing;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;

    public class MailConfigWeb : Page
    {
        protected Button btnSave;
        protected DropDownList ddlAuthMode;
        protected Label lblPasswordSet;
        protected HtmlTableRow MessageRow;
        protected UserPrincipal Principal = null;
        protected RegularExpressionValidator RegularExpressionValidator1;
        protected RequiredFieldValidator RequiredFieldValidator1;
        protected RequiredFieldValidator RequiredFieldValidator2;
        protected RequiredFieldValidator RequiredFieldValidator4;
        protected TextBox txtEmail;
        protected TextBox txtSmtpPassword;
        protected TextBox txtSmtpServer;

        private void btnSave_Click(object sender, EventArgs e)
        {
            string message = "";
            try
            {
                this.SaveSysMailConfig();
                message = "保存设置成功。";
                this.lblPasswordSet.Text = "密码已设置";
            }
            catch (Exception ex)
            {
                message = string.Format("保存设置失败，可能原因：{0}", ex.Message);
            }
            this.ShowMessage(Color.Red, message);
        }

        private void InitializeComponent()
        {
            this.btnSave.Click += new EventHandler(this.btnSave_Click);
            base.Load += new EventHandler(this.Page_Load);
        }

        private void InitPage()
        {
            this.txtEmail.Text = MailConfig.SmtpSender;
            this.txtSmtpServer.Text = MailConfig.SmtpHost;
            string smtpAuthMode = MailConfig.SmtpAuthMode;
            ListItem li = this.ddlAuthMode.Items.FindByValue(smtpAuthMode);
            if (li != null)
            {
                li.Selected = true;
            }
        }

        protected override void OnInit(EventArgs e)
        {
            this.InitializeComponent();
            base.OnInit(e);
            this.Principal = this.Context.User as UserPrincipal;
        }

        private void Page_Load(object sender, EventArgs e)
        {
            if (this.Principal.SysUser.IsAdmin == 0)
            {
                throw new ApplicationException(string.Format("User: {0} not has privilege to access this page.", this.Principal.SysUser.LoginName));
            }
            if (!this.Page.IsPostBack)
            {
                this.MessageRow.Visible = false;
                this.InitPage();
            }
        }

        private void SaveSysMailConfig()
        {
            string sysEmail = this.txtEmail.Text.Trim();
            ManageSystemConfig.UpdateSystemConfig("sysmail.smtpsender", sysEmail);
            string smtpHost = this.txtSmtpServer.Text.Trim();
            ManageSystemConfig.UpdateSystemConfig("sysmail.smtphost", smtpHost);
            string smtpPass = this.txtSmtpPassword.Text.Trim();
            ManageSystemConfig.UpdateSystemConfig("sysmail.smtppass", smtpPass);
            string smtpAuthMode = this.ddlAuthMode.SelectedValue;
            ManageSystemConfig.UpdateSystemConfig("sysmail.smtpauthmode", smtpAuthMode);
        }

        private void ShowMessage(Color fcolor, string message)
        {
            this.MessageRow.Visible = true;
            this.MessageRow.Cells[0].InnerHtml = string.Format("<FONT COLOR='{0}' STYLE='FONT-SIZE:12px'>&nbsp;{1}</FONT>", fcolor.Name, message);
        }
    }
}

